
/** 
Copyright (c) 2010, Sheikh Abdul Wahid Ahmed
Details @ http://extremecodeworld.codeplex.com/license
**/

using System;
using EXtremecode.Utils;
using System.Collections;
using System.Collections.Generic;

namespace EXtremecode.DataAccessLayer
{
	/// <summary>
	/// Summary description for TypeHash.
	/// </summary>
	public class FieldInfo
	{
		private string m_strName;
		private string m_strType;
		private bool m_bIsKey;
		private bool m_bIsComputed;
		private string m_intTokenId;
		private bool m_bIsNative;
		private bool m_bIsRange;
		private bool m_bIsUnique;

		
		
		public FieldInfo(string name,string type,bool isKey,bool isUnique,string tokenId,bool isComputed,bool isNative,bool isRange):this(name,type,isKey,isUnique,tokenId,isComputed,isNative,isRange,null)
		{}
		public FieldInfo(string name,string type,bool isKey,bool isUnique,string tokenId,bool isComputed,bool isNative,bool isRange,object value)
		{
			m_strName=name;
			m_strType=type;
			m_bIsKey=isKey;
			m_bIsComputed=isComputed;
			m_intTokenId=tokenId;
			m_bIsNative=isNative;
			m_bIsRange=isRange;
			m_bIsUnique=isUnique;
		}

		public string Name
		{
			get
			{
				return m_strName; 
			}
		}
		public string Type
		{
			get
			{
				return m_strType;
			}
		}
		public string TokenId
		{
			get
			{
				return m_intTokenId;
			}
		}
		public bool IsKey
		{
			get
			{
				return m_bIsKey;
			}
		}
		public bool IsUnique
		{
			get
			{
				return m_bIsUnique;
			}
		}
		public bool IsComputed
		{
			get
			{
				return m_bIsComputed;
			}
		}
		public bool IsNative
		{
			get
			{
				return m_bIsNative;
			}
		}
		public bool IsRange
		{
			get
			{
				return m_bIsRange;
			}
		}
		
	}
	public class FieldInfoCollection:Hashtable
	{
        private SortedList<string, FieldInfo> slKeyFields = new SortedList<string, FieldInfo>();
        private List<FieldInfo> tokenFields = new List<FieldInfo>();
        private List<FieldInfo> specialFields = new List<FieldInfo>();
        private List<FieldInfo> nativeFields = new List<FieldInfo>();
        private List<FieldInfo> referencedFields = new List<FieldInfo>();
        string tableName,viewName,dataTableName;
		public FieldInfoCollection(string dataTableName,string tableName,string viewName)
		{
			this.dataTableName=dataTableName;
			this.tableName=tableName;
			this.viewName=viewName;
		}
		
		public FieldInfo this[string key]
		{
			get
			{
				return (base[key] as FieldInfo);
				
			}
			
		}

		public IList<FieldInfo> KeyFields
		{
			get
			{
                return slKeyFields.Values;
            }
		}
        public IList<FieldInfo> TokenFields
		{
			get
			{
                return tokenFields;
			}
		}

        public IList<FieldInfo> SpecialFields
        {
            get
            {
                return specialFields;
            }
        }

        public IList<FieldInfo> ReferencedFields
        {
            get
            {
                return referencedFields;
            }
        }


        public IList<FieldInfo> NativeFields
        {
            get
            {
                return nativeFields;
            }
        }
		
		public  void Add(string key, FieldInfo value)
		{
			if(base.Contains(key))throw new Exception(string.Format("[UE] FieldInfo with the key '{0}', already exsists",key));
			base.Add(key,value);
			
			//key
			if(value.IsKey)
			{
				slKeyFields.Add(key,value);
			}
			
			//token
			if(value.TokenId != null && value.TokenId != string.Empty)
			{
				tokenFields.Add(value);
			}

            //native fields
            if (value.IsNative)
            {
                nativeFields.Add(value);
            }
            else //refereneced fields
            {
                referencedFields.Add(value);
            }

            //special fields
            if (key.Equals(DataAccessLayer.Configurations.Provider.TimestampColumnName,
                    StringComparison.OrdinalIgnoreCase))
            {
                specialFields.Add(value);
            }
		}
		public string TableName
		{
			get
			{
				return tableName; 
			}
		}
		public string ViewName
		{
			get
			{
				return viewName;
			}
		}
		public string DataTableName
		{
			get
			{
				return dataTableName;
			}
		}
		
		
		
		#region Hidden Methods
		public override void Add(object key, object value)
		{
			throw new Exception("[UE] method is not allowed");
		}
		public override void CopyTo(Array array, int arrayIndex)
		{
			throw new Exception("[UE] method is not allowed");

		}
		#endregion
	}
}
